<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Tab选项卡</title>
  <style>
    .container {
      max-width: 800px;
      margin: 30px auto;
      border-radius: 4px;
      box-shadow: 0 0 4px 0px rgba(0, 0, 0, 0.2);
      padding: 16px;
    }

    .tabs .tab-header {
      position: relative;
      display: flex;
      font-size: 14px;
      color: #303030;
      line-height: 2;
      font-weight: 500;
      border-bottom: 1px solid #ddd;
    }

    .tabs .tab-item {
      margin-right: 30px;
      margin-bottom: -2px;
      cursor: pointer;
    }

    .tabs .line {
      position: absolute;
      top: 100%;
      transform: translateX(0);
      /* width: 0; */
      height: 2px;
      background-color: #409eff;
      transition: all .3s;
    }

    .tabs .tab-item.active {
      color: #409eff;
      /* border-bottom: 2px solid #409eff; */
    }

    .tabs .tab-panel {
      padding: 16px 0;
      font-size: 16px;
      display: none;
    }

    .tabs .tab-panel.active {
      display: block;
    }
  </style>
</head>

<body>
  <div class="container">
    <h2>Tab选项卡</h2>
    <div class="tabs">
      <div class="tab-header">
        <span class="tab-item active">用户管理</span>
        <span class="tab-item">配置管理</span>
        <span class="tab-item">角色管理</span>
        <span class="tab-item">定时任务补偿</span>
        <span class="line"></span>
      </div>
      <div class="tab-content">
        <div class="tab-panel active">用户管理</div>
        <div class="tab-panel">配置管理</div>
        <div class="tab-panel">角色管理</div>
        <div class="tab-panel">定时任务补偿</div>
      </div>
    </div>
  </div>

  <script>
    class Tabs {
      constructor($container) {
        this.$container = $container
        this.$$tabItems = $container.querySelectorAll('.tab-item')
        this.$$tabPanels = $container.querySelectorAll('.tab-panel')
        this.$line = $container.querySelector('.line')

        this.init()
        this.bind()
      }
      init() {
        this.$line.style.width = this.$$tabItems[0].offsetWidth + 'px'

      }
      bind() {
        let self = this
        this.$$tabItems.forEach($tab => {
          $tab.onclick = function () {
            self.$$tabItems.forEach($tab => $tab.classList.remove('active'))
            this.classList.add('active')
            // offsetLeft表示相对于relative元素的左边的宽度，否则会相对于body
            self.$line.style.width = this.offsetWidth + 'px'
            // $line.style.left = this.offsetLeft + 'px'
            self.$line.style.transform = `translateX(${this.offsetLeft}px)`
            let index = [...self.$$tabItems].indexOf(this)
            self.$$tabPanels.forEach($tab => $tab.classList.remove('active'))
            self.$$tabPanels[index].classList.add('active')
          }
        })
      }
    }

    document.querySelectorAll('.tabs').forEach($container => new Tabs($container))

    // new Tabs(document.querySelectorAll('.tabs')[0])
    // new Tabs(document.querySelectorAll('.tabs')[1])

  </script>
</body>

</html>